package com.eliza.v.server.Tcp

import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.VertxOptions
import io.vertx.core.net.*


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 2/1/22
 * @Software: IntelliJ IDEA
 */
class TcpClient : AbstractVerticle() {
    lateinit var client: NetClient
    fun init() {
        val options = NetClientOptions().setConnectTimeout(10000).apply {
            logActivity = true
//            isSsl = true
            //
            isTrustAll = true
            //未设置 trustAll，则必须配置客户端信任库，并且应该包含客户端信任的服务器的证书。
//            hostnameVerificationAlgorithm = "HTTPS";

        }


        vertx = Vertx.vertx(VertxOptions().setWorkerPoolSize(40))

        client = vertx.createNetClient(options)
    }

    fun connect() {
        client.connect(8081, "localhost") { res ->
            if (res.succeeded()) {
                println("client:Connected!")
                //用Socket操作数据
                val socket: NetSocket = res.result()
                socket.write("向服务器写数据")

                socket.handler {
                    println(it.toString())
                }

            } else {
                println("Failed to connect: " + res.cause().message)
            }
        }
    }
}